package com.demo.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * 分页格式转换（mybatisplus->github）
 *
 * @author wangfengchen
 */
@Slf4j
public class MybatisPageUtils {

    /**
     * mybatis 的分页转换为api的分页
     * <p>
     * 由于mybatis分页类com.baomidou.mybatisplus.extension.plugins.pagination.Page 无法在api/controller使用,此处查询结果返回前手动转为com.github.pagehelper.PageInfo
     * </p>
     *
     * @param pageList com.baomidou.mybatisplus.extension.plugins.pagination.Page类型
     * @param <T>
     * @return com.github.pagehelper.PageInfo类型
     */
    public static <T> PageInfo<T> convertPageResult(Page<T> pageList) {
        if (pageList == null) {
            return PageInfo.emptyPageInfo();
        }
        // 当前页
        Long current = pageList.getCurrent();
        // 当前分页总页数
        Long totalPages = pageList.getPages();
        // 总数
        Long total = pageList.getTotal();
        // 每页显示条数，默认 10
        Long pageSize = pageList.getSize();
        // Params: page – 页码，0表示第一页, pageSize – 每页结果数, total – 结果总数
        PageInfo result = new PageInfo();
        // 当前页的条数
        result.setSize(CollectionUtils.isEmpty(pageList.getRecords()) ? 0 : pageList.getRecords().size());
        result.setTotal(total);
        result.setPageSize(pageSize.intValue());
        result.setPageNum(current.intValue());
        result.setPages(totalPages.intValue());

        result.setStartRow((current - 1) * pageSize + 1);

        result.setEndRow(result.getStartRow() + result.getSize() - 1);

        result.setIsLastPage(current.equals(totalPages));
        result.setHasNextPage(current.intValue() < totalPages.intValue());

        result.setHasPreviousPage(result.getPageNum() > 1);

        if (result.isHasNextPage()) {
            result.setNextPage(current.intValue() + 1);
        } else {
            result.setNextPage(current.intValue());
        }

        if (result.isHasPreviousPage()) {
            result.setPrePage(current.intValue() - 1);
        } else {
            result.setPrePage(current.intValue());
        }
        result.setIsFirstPage(current.longValue() == 1L);
        return result;
    }

    /**
     * 跨类型转换page
     *
     * @param pageList
     * @param records
     * @param <T>
     * @return
     */
    public static <T> PageInfo<T> convertPageResult(Page pageList, List<T> records) {
        if (pageList == null || records == null) {
            return PageInfo.emptyPageInfo();
        }
        // 当前页
        Long current = pageList.getCurrent();
        // 当前分页总页数
        Long totalPages = pageList.getPages();
        // 总数
        Long total = pageList.getTotal();
        // 每页显示条数，默认 10
        Long pageSize = pageList.getSize();
        // Params: page – 页码，0表示第一页, pageSize – 每页结果数, total – 结果总数
        PageInfo result = new PageInfo();
        // 当前页的条数
        result.setSize(CollectionUtils.isEmpty(pageList.getRecords()) ? 0 : pageList.getRecords().size());
        result.setTotal(total);
        result.setPageSize(pageSize.intValue());
        result.setPageNum(current.intValue());
        result.setPages(totalPages.intValue());

        result.setStartRow((current - 1) * pageSize + 1);

        result.setEndRow(result.getStartRow() + result.getSize() - 1);

        result.setIsLastPage(current.equals(totalPages));
        result.setHasNextPage(current.intValue() < totalPages.intValue());

        result.setHasPreviousPage(result.getPageNum() > 1);

        if (result.isHasNextPage()) {
            result.setNextPage(current.intValue() + 1);
        } else {
            result.setNextPage(current.intValue());
        }

        if (result.isHasPreviousPage()) {
            result.setPrePage(current.intValue() - 1);
        } else {
            result.setPrePage(current.intValue());
        }
        result.setIsFirstPage(current.longValue() == 1L);

        result.setList(records);
        return result;
    }


}
